package com.thuanhoang.boo.adapter;

import java.util.List;

import thirdparty.SquareImageView;
import uk.co.senab.photoview.PhotoView;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

/***
 * adapter view pager class
 * 
 * @author MacOSX
 * 
 */
public class ImageCoverAdapter extends PagerAdapter {
	//
	private List<String> listImages;
	private boolean isZoom = true;

	// PhotoView photoView;
	// private PhotoViewAttacher mAttacher;

	public ImageCoverAdapter(Context context, List<String> sImages) {

		// get list image
		this.listImages = sImages;
	}

	public boolean isZoom() {
		return isZoom;
	}

	public void setZoom(boolean isZoom) {
		this.isZoom = isZoom;
	}

	@Override
	public int getCount() {
		return listImages.size();
	}

	@Override
	public View instantiateItem(ViewGroup container, int position) {
		SquareImageView photoView;
//		if (isZoom) {
//			photoView = new PhotoView(container.getContext());
//			photoView.setScaleType(ScaleType.FIT_XY);
//		} else {
			photoView = new SquareImageView(container.getContext());
			photoView.setScaleType(ScaleType.CENTER);

		//}

		// photoView.
		String imagePath = listImages.get(position);
		photoView.setTag(position);

		// use image loader to display cover
		DisplayImageOptions options = new DisplayImageOptions.Builder()
		.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
		.cacheInMemory(true)
		.cacheOnDisc(true)
		.bitmapConfig(Bitmap.Config.RGB_565).build();
		ImageLoader.getInstance().displayImage(imagePath, photoView,options);
		//ImageLoader

		// Now just add PhotoView to ViewPager and return it
		container.addView(photoView, LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		//
		return photoView;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	// update adapter
	public void updateAdapter(List<String> sImages) {
		// set list images
		this.listImages = sImages;
		this.notifyDataSetChanged();
	}

}